package com.hspedu.newData.dmsxl.字符串;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-07 
 * @Version: 1.0
 */

public class LC345反转字符串中的元音字母 {

    public String reverseVowels(String s) {

        int i = 0;
        int j = s.length() - 1;

        Set<Character> set = new HashSet<>();

        set.add('A');
        set.add('E');
        set.add('I');
        set.add('O');
        set.add('U');
        set.add('a');
        set.add('e');
        set.add('i');
        set.add('o');
        set.add('u');

        StringBuilder sb = new StringBuilder(s);

        while(i<j){
            while (!set.contains(sb.charAt(i)) && i < j){
                i++;
            }

            while (!set.contains(sb.charAt(j)) && i < j){
                j--;
            }
            if (i<j){
                sb.setCharAt(i,s.charAt(j));
                sb.setCharAt(j,s.charAt(i));
                i++;
                j--;
            }
        }
        return sb.toString();
    }
}
